//
//  DrawbackController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/8/30.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "DrawbackController.h"
#import "OilOrderModel.h"
#import "DrawBackSuccessController.h"

@interface DrawbackController ()
{
    MBProgressHUD *progressHUD;
}

@end

@implementation DrawbackController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"%@",self.oidStr);
    NSLog(@"%@",self.type);

    [self buildView];
}

- (void)buildView
{
    self.title = @"申请退款";
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(drawbackSubmitDidClick)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:15],NSFontAttributeName, nil] forState:UIControlStateNormal];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
    
    CGFloat margin = 20;
    UIView *block1View = [[UIView alloc] initWithFrame:CGRectMake(margin,margin, WIDTH-margin *2,40)];
    [self.view addSubview:block1View];
    block1View.backgroundColor = BACKGROUND_COLOR;
    
    UIImageView *starImg1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_star"]];
    [block1View addSubview:starImg1];
    starImg1.frame = CGRectMake(15,15,5,5);
    
    UILabel *label1 = [[UILabel alloc] init];
    [block1View addSubview:label1];
    label1.text = @"申请服务          退款";
    label1.textColor = [UIColor grayColor];
    label1.font = FONT(14);
    
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(block1View.mas_centerY);
        make.left.equalTo (block1View.mas_left).with.offset(25);
        
    }];
    
    UIView *block2View = [[UIView alloc] initWithFrame:CGRectMake(margin,CGRectGetMaxY(block1View.frame)+margin,block1View.width,40)];
    [self.view addSubview:block2View];
    block2View.backgroundColor = BACKGROUND_COLOR;
    
    UIImageView *starImg2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_star"]];
    [block2View addSubview:starImg2];
    starImg2.frame = CGRectMake(15,15,5,5);
    
    UILabel *label2 = [[UILabel alloc] init];
    [block2View addSubview:label2];
    label2.text = [NSString stringWithFormat:@"退款金额:          ¥%@",self.rcMoney];
    label2.textColor = [UIColor grayColor];
    label2.font = FONT(14);
    
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(block2View.mas_centerY);
        make.left.equalTo (block2View.mas_left).with.offset(25);
        
    }];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(margin,CGRectGetMaxY(block2View.frame)+margin,WIDTH-margin*2,40)];
    [self.view addSubview:button];
    [button setTitle:@"提 交" forState:UIControlStateNormal];
    [button setBackgroundColor:DEFAULT_COLOR];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.layer.cornerRadius = 3;
    button.layer.masksToBounds = YES;
    button.titleLabel.font = FONT(15);
    [button addTarget:self action:@selector(drawbackSubmitDidClick) forControlEvents:UIControlEventTouchUpInside];
}


- (void)drawbackSubmitDidClick
{
    progressHUD = [MBProgressHUD show:self.view andMessage:@"正在退款"];
    [self.view addSubview:progressHUD];

    NSDictionary *params = @{
                             @"oid":self.oidStr,
                             @"type":_type,
                             @"username":USERNAME,
                             };
    NSLog(@"%@",params);
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Order/Refund" completeBlock:^(id object) {

        NSLog(@"%@",object);
        if (JudgeBackDataCode) {
            DrawBackSuccessController *DrawBackSuccessVC = [[DrawBackSuccessController alloc] init];
            DrawBackSuccessVC.type = _type;
            [self.navigationController pushForm:self.navigationController pushToAndHidden:DrawBackSuccessVC];
        }else{
            
            NSString *message = [object[@"message"] objectForKey:@"view"];
            MBProgressHUD *hud = [MBProgressHUD showMessage:message toView:self.view dimBackground:NO AndCompleteBlock:nil];
            [self.view addSubview:hud];
            
        }

        
    }];
    
    [progressHUD hide:YES];
    
}

@end

